<%@page import="java.math.BigDecimal"%>
<%@page import="multiface.awwadco.jpa.Utente"%>
<%
    Utente ut = (Utente) session.getAttribute("admin");
    if (ut == null) {
        response.sendRedirect(multiface.web.util.ServletUtils.getBaseUrl(request) + "/admin/index.jsp?err=y");
    }
%>
<%@page import="multiface.web.util.PagerList"%>
<%@page import="javax.persistence.Query"%>
<%@page import="java.util.LinkedList"%>
<%@page import="java.util.Iterator"%>
<%@page import="multiface.awwadco.jpa.Prodotto"%>
<%@page import="java.util.List"%>
<%@page import="javax.persistence.RollbackException"%>
<%@page import="javax.persistence.EntityManager"%>
<%@page import="javax.persistence.Persistence"%>
<%@page import="javax.persistence.EntityManagerFactory"%>
<%@page import="multiface.awwadco.jpa.Categoria"%>
<%@page import="multiface.awwadco.jpa.File"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@page import="multiface.awwadco.jpa.PersistenceManager"%>

<script type="text/javascript">
    $(document).ready(function(){
        $("#formBox").validate();
        nicEditors.allTextAreas({buttonList : ['bold','italic','underline','ul','ol']});
    });
    
    $("#homepage").live('click',function() {
        if($("#homepage:checked").val()=='on'){
            $('#homepage_val').val("true");
            $("#homepage_span").html('Show in HomePage');
        }else{
            $('#homepage_val').val("false");
            $("#homepage_span").html('Hidden in HomePage');
        }
    });
    
    $("#visibile_prezzo").live('click',function() {
        if($("#visibile_prezzo:checked").val()=='on'){
            $('#visibile_prezzo_val').val("true");
            $("#visibile_prezzo_span").html('Show in HomePage');
        }else{
            $('#visibile_prezzo_val').val("false");
            $("#visibile_prezzo_span").html('Hidden in HomePage');
        }
    });
    
    $("#offerta").live('click',function() {
        if($("#offerta:checked").val()=='on'){
            $('#offerta_val').val("true");
        }else{
            $('#offertaval').val("false");
        }
    });
    
    $("#select_condition").change(function(data) {
        var sel = $("#select_condition option:selected").val();
        if(sel == "-1"){
             $("#condizioni").val("");
            $("#condizioni").attr("disabled", false);
        }else{
            $("#condizioni").val(sel);
            $("#condizioni").attr("disabled", true);
        }
    });
</script>

<div class="box" id="box-1">
    <form class="formBox" id="formBox" method="post" action="">
        <%
            EntityManagerFactory emf = PersistenceManager.getInstance().getEntityManagerFactory();
            EntityManager em = emf.createEntityManager();

            String pid = request.getParameter("pid");

            Prodotto p = em.createNamedQuery("Prodotto.findByIdprodotto", Prodotto.class).setParameter("idprodotto", Integer.parseInt(pid)).getSingleResult();

            em.close();
            //emf.close();

        %>
        <div class="title">Modify Product: <%=p.getNome()%></div>
        <br>
        <input id="idprodotto" name="idprodotto" type="hidden" value="<%=pid%>"/>


        <div class="inputs">
            <label for="nome">Name</label>
            <input name="nome" id="nome" class="required" maxlength="255" type="text" value="<%=p.getNome()%>"/>
        </div>

        <div class="inputs">
            <label style="height:50px;margin-right: 6px;" for="nome">Short Description</label>
            <textarea cols="100" style="height:100px;" name="descrizione_homepage" id="descrizione_homepage"><%=p.getDescrizioneHomepage()%></textarea>
        </div>

        <div class="inputs">
            <label style="height: 50px;margin-right: 6px;" for="nome">Description</label>
            <textarea cols="100" style="height:200px;" name="descrizione" id="descrizione"><%=p.getDescrizione()%></textarea>
        </div>
        
        <div class="inputs">
            <label for="modello">Model</label>
            <input name="modello" id="modello" class="required" maxlength="255" type="text" value="<%=p.getModello()%>"/>
        </div>

        <div class="inputs">
            <label for="condizioni">Conditions</label>
            <input value="<%=p.getCondizioni()%>" name="condizioni" id="condizioni" class="required" maxlength="255" type="text"/>
            <select id="select_condition">
                <option value="-1">   </option>
                <option value="Excellent">Excellent</option>
                <option value="Good">Good</option>
                <option value="Refurbished">Refurbished</option>
            </select>
        </div>

        <div class="inputs">
            <label for="homepage">HomePage</label>
            <%
                if (p.getHomepage() == true) {
            %>
            <input style="width: 16px;" name="homepage" id="homepage" type="checkbox" checked="checked"/>
            <span id="homepage_span" style="font-size: 10px; font-style: italic; color:#999;">Show in HomePage</span>
            <input type="hidden" name="homepage_val" id="homepage_val" value="true"/>
            <%        } else {
            %>
            <input style="width: 16px;" name="homepage" id="homepage" type="checkbox" />
            <span id="homepage_span" style="font-size: 10px; font-style: italic; color:#999;">Hidden in HomePage</span>
            <input type="hidden" name="homepage_val" id="homepage_val" value="false"/>
            <%            }
            %>
        </div>

        <div class="inputs">
            <label for="anno">Year</label>
            <select id="anno">
                <%
                    out.println("<option value=\"-1\">NONE</option>");
                    String anno = p.getAnno();
                    if (anno.length() != 0) {
                        for (int i = 1960; i <= 2031; i++) {
                            if (i == Integer.parseInt(anno)) {
                                out.println("<option selected value=\"" + i + "\">" + i + "</option>");
                            } else {
                                out.println("<option value=\"" + i + "\">" + i + "</option>");
                            }
                        }
                    } else {
                        out.println("<option value=\"\">NONE</option>");
                        for (int i = 1960; i <= 2031; i++) {
                            out.println("<option value=\"" + i + "\">" + i + "</option>");
                        }
                    }
                %>
            </select>
        </div>

        <div class="inputs">
            <label for="moneta">Money</label>
            <select id="moneta">
                <%
                    if (p.getMoneta().equals("DOLLARO")) {
                        out.println("<option selected value=\"DOLLARO\">US Dollar ($)</option>");
                        out.println("<option value=\"EURO\">Euro (€)</option>");
                    } else {
                        out.println("<option value=\"DOLLARO\">US Dollar ($)</option>");
                        out.println("<option selected value=\"EURO\">Euro (€)</option>");
                    }
                %>
            </select>
        </div>

        <div class="inputs">
            <label for="codice">Code</label>
            <input name="codice" id="codice" class="required" maxlength="10" type="text" value="<%=p.getCodice()%>"/>
        </div>

        <div class="inputs">
            <label for="prezzo">Price</label>
            <%
                if (p.getPrezzo() != null) {
            %>
            <input name="prezzo" id="prezzo" type="text" value="<%=p.getPrezzo()%>"/>
            <%
            } else {
            %>
            <input name="prezzo" id="prezzo" type="text"/>
            <%                    }
                if (p.getVisibilePrezzo() == true) {
            %>
            <input style="width: 16px" name="visibile_prezzo" id="visibile_prezzo" type="checkbox" checked="checked"/>
            <span id="visibile_prezzo_span" style="font-size: 10px; font-style: italic; color:#999;">Show in HomePage</span>
            <input type="hidden" name="visibile_prezzo_val" id="visibile_prezzo_val" value="true"/>
            <%        } else {
            %>
            <input style="width: 16px" name="visibile_prezzo" id="visibile_prezzo" type="checkbox"/>
            <span id="visibile_prezzo_span" style="font-size: 10px; font-style: italic; color:#999;">Hidden in HomePage</span>
            <input type="hidden" name="visibile_prezzo_val" id="visibile_prezzo_val" value="false"/>
            <%            }
            %>
        </div>

        <div class="inputs">
            <label for="pezzi">Pieces in Stock</label>
            <input name="pezzi" id="pezzi" type="text" value="<%=p.getPezziMagazzino()%>"/>
        </div>

        <div class="inputs">
            <label for="offerta">Promo</label>
            <%
                if (p.getOfferta() == true) {
            %>
            <input style="width: 16px;" name="offerta" id="offerta" type="checkbox" checked="checked"/>
            <input type="hidden" name="offerta_val" id="offerta_val" value="true"/>
            <%        } else {
            %>
            <input style="width: 16px;" name="offerta" id="offerta" type="checkbox"/>
            <input type="hidden" name="offerta_val" id="offerta_val" value="false"/>
            <%            }
            %>
        </div>

        <div class="inputs">
            <label for="visualizzazioni">Number Visit</label>
            <input name="visualizzazioni" id="visualizzazioni" type="text" value="<%=p.getVisualizzazioni()%>"/>
        </div>

        <div id="result"></div>
    </form>
</div>


